package com.glory.brepda.activity.technicalservice;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.glory.brepda.R;
import com.glory.brepda.activity.check.QCDetailActivity;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.AbnormalMaintenanceBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AMDetailActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.flCheckResult)
    FrameLayout flCheckResult;
    @BindView(R.id.tvCheckResult)
    TextView tvCheckResult;
    @BindView(R.id.ivCheckResult)
    ImageView ivCheckResult;
    @BindView(R.id.etAbNormalHandleRecord)
    FreeUI_ClearEditText etAbNormalHandleRecord;
    @BindView(R.id.etAbnormalReason)
    FreeUI_ClearEditText etAbnormalReason;
    @BindView(R.id.etProductDisposal)
    FreeUI_ClearEditText etProductDisposal;
    @BindView(R.id.etMachineDisposal)
    FreeUI_ClearEditText etMachineDisposal;
    @BindView(R.id.tvDisposaleonel)
    TextView tvDisposaleonel;
    @BindView(R.id.tvDisposalDate)
    TextView tvDisposalDate;

    private AbnormalMaintenanceBean.XPMSABNORMALHANDLEFORMSDTO amMessage;
    private List<String> mCheckResultList = new ArrayList<>();
    private FreeUI_GeneralPop mCheckResultPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_amdetail;
    }

    @Override
    protected void initView() {

        setMainLayout(llMain);
        tvTitle.setText("保养异常处置详情");
        tvBaseSubmit.setText("提交");
        tvBaseSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                amMessage.setMACHINEDISPOSAL(getText(etMachineDisposal));
                amMessage.setPRODUCTDISPOSAL(getText(etProductDisposal));
                amMessage.setABNORMALREASON(getText(etAbnormalReason));
                amMessage.setABNORMALHANDLERECORD(getText(etAbNormalHandleRecord));
                amMessage.setABNORMALHANDLERECORD(tvCheckResult.getText().toString());
                submit();
            }
        });

        amMessage = (AbnormalMaintenanceBean.XPMSABNORMALHANDLEFORMSDTO) getIntent().getSerializableExtra("amMessage");
        assert amMessage != null;
        tvDisposalDate.setText(amMessage.getDISPOSALDATE());
        tvDisposaleonel.setText(amMessage.getDISPOSALPERSONEL());
        etMachineDisposal.setText(amMessage.getMACHINEDISPOSAL());
        etProductDisposal.setText(amMessage.getPRODUCTDISPOSAL());
        etAbnormalReason.setText(amMessage.getABNORMALREASON());
        etAbNormalHandleRecord.setText(amMessage.getABNORMALHANDLERECORD());
        tvCheckResult.setText(amMessage.getCHECKRESULT());

    }

    @Override
    protected void initData() {

        mCheckResultList.add("OK");
        mCheckResultList.add("FAIL");

        flCheckResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCheckResultPop != null) {
                    if (mCheckResultPop.isShowing()) {
                        mCheckResultPop.dismiss();
                    }
                } else {
                    mCheckResultPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flCheckResult.getWidth(), mCheckResultList, ivCheckResult, (codeS, i) -> {
                        tvCheckResult.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivCheckResult);
                mCheckResultPop.showAsDropDown(flCheckResult, 0, 0);
            }
        });

    }

    private void submit() {

        FreeUI_EntityCallBack<BaseResponseBean<AbnormalMaintenanceBean>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<AbnormalMaintenanceBean>>
                (new TypeToken<BaseResponseBean<AbnormalMaintenanceBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AbnormalMaintenanceBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {

                    showToast("提交成功", true);
                    setResult(Activity.RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AbnormalMaintenanceBean>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSABNORMALHANDLEFORMEXCUTE");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "EXCUTE");
        List<AbnormalMaintenanceBean.XPMSABNORMALHANDLEFORMSDTO> amMessageList = new ArrayList<>();
        amMessageList.add(amMessage);
        map.add("XPMSABNORMALHANDLEFORMS", new JsonParser().parse(new Gson().toJson(amMessageList)).getAsJsonArray());
        NetUtils.requestPostNet("IF.PMSABNORMALHANDLEFORMEXCUTE", "Common", AMDetailActivity.this, map, callBack);


    }


}